/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.loader;

import id.duapuluhdua.dakidaki.activities.ActivityMain;
import id.duapuluhdua.dakidaki.gps.GpsLocationStatus;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 *
 */
public class TrackLoader extends Loader
{
  /** The fields */
  private TrackCollection mTrackList;
  private TrackItem mSelectedItem;

  private final Boolean bSelectedObjectMutex = false;

  public TrackLoader(ActivityMain parent, Screen screen, GpsLocationStatus gpsLocationStatus)
  {
    super(parent);

    mTrackList = new TrackCollection(parent, screen, gpsLocationStatus);
  }

  /**
   * Returns the track collection.
   *
   * @return a track list.
   */
  public TrackCollection items()
  {
    return mTrackList;
  }

  @Override
  protected void loadAllItems()
  {
    mTrackList.loadAllItems();
  }

  @Override
  protected boolean selectObject(long objectId)
  {
    boolean bSuccess = false;

    synchronized(mTrackList)
    {
      // Get a track item from the collection.
      final TrackItem trackItem = mTrackList.getLoaderItem(objectId);

      if (trackItem != null) {
        if (!trackItem.isEmpty()) { // the points are not empty.
          this.setSelectedTrack(trackItem);
          mState.setSelectedTrack(objectId, true);

          trackItem.getLayer().setResetViewport();

//          StatusMessage.showText(trackItem.getDescription());

          bSuccess = true;
        }
      }
    }
    if (!bSuccess) {
      clearTrackSelection();
    }
    return bSuccess;
  }

  /**
   *
   */
  public void selectOpenTrack()
  {
    final long openTrackId = mState.getOpenTrackId();
    if (openTrackId != -1) {
      synchronized(mTrackList)
      {
        // Get track from list
        final TrackItem trackItem = mTrackList.getLoaderItem(openTrackId);

        // Show track
        if (trackItem != null) {
          setSelectedTrack(trackItem);

          mState.setSelectedTrack(openTrackId, false);

          // Show status message of selected track
          // TODO Change to Toast
//          StatusMessage.showText(trackItem.getDescription());
        }
      }
    }
  }

  /**
   * Unload the current selection of the track in this loader as well as
   * in the global state of the application.
   */
  public void clearTrackSelection()
  {
    setSelectedTrack(null);
    mState.setSelectedTrack(-1, false);
  }

  public TrackItem getSelectedTrack()
  {
    synchronized(bSelectedObjectMutex)
    {
      return mSelectedItem;
    }
  }

  /* Set the current track. */
  private void setSelectedTrack(TrackItem item)
  {
    synchronized(bSelectedObjectMutex)
    {
      mSelectedItem = item;
    }
  }
}
